package com.test.daily.leetcode.y2022.m06.day0622.v02;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.Queue;

/**
 * @author Tom on 2022/6/22
 */
public class Solution {

    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    public static int findBottomLeftValue(TreeNode root) {
        HashMap<TreeNode, Integer> hashMap = new HashMap<>();
        int maxLevel = 0;
        TreeNode leftest = root;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        hashMap.put(root, 0);
        while (!queue.isEmpty()) {
            TreeNode cur = queue.poll();
            int curLevel = hashMap.get(cur);
            if (curLevel != maxLevel) {
                maxLevel = curLevel;
                leftest = cur;
            }
            if (cur.left != null) {
                hashMap.put(cur.left, curLevel + 1);
                queue.add(cur.left);
            }
            if (cur.right != null) {
                hashMap.put(cur.right, curLevel + 1);
                queue.add(cur.right);
            }
        }
        return leftest.val;
    }
}
